package com.sikaryofficial.backend.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author : qinjinyuan
 * @desc : MybatisPlus配置类
 * @date : 2023/11/25 14:35
 */
@Configuration
@EnableConfigurationProperties({TenantFilterProperties.class})
public class MybatisPlusConfig {

    private final TenantFilterProperties tenantFilterProperties;

    public MybatisPlusConfig(TenantFilterProperties tenantFilterProperties) {
        this.tenantFilterProperties = tenantFilterProperties;
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分表 DynamicTableNameInnerInterceptor： 根据租户，进行表的切分
        // 分租户 TenantLineInnerInterceptor ： 根据租户，进行查询字段的过滤
        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantHandler(tenantFilterProperties)));

        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        //设置最大单页限制数量，默认 500 条，-1 不受限制:现设置为2000
        paginationInnerInterceptor.setMaxLimit(2000L);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }
}
